<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?
	'SA_SALESTRANSVIEW' : 'SA_SALESBULKREP';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Print Sales Orders
// ----------------------------------------------------------------
$path_to_root="..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/taxes/tax_calc.inc");
include_once($path_to_root . "/sales/includes/db/uang_muka_db.inc");


print_sales_orders();

$print_as_quote = 0;

function print_sales_orders()
{
	global $path_to_root, $print_as_quote;

	include_once($path_to_root . "/reporting/includes/pdf_report.inc");

	$from = $_POST['PARAM_0'];
	$to = $_POST['PARAM_1'];
	$currency = $_POST['PARAM_2'];
	$email = $_POST['PARAM_3'];
	$print_as_quote = $_POST['PARAM_4'];
	$comments = $_POST['PARAM_5'];

	if ($from == null)
		$from = 0;
	if ($to == null)
		$to = 0;
	$dec = user_price_dec();

	//$cols = array(1,4, 60, 225, 300, 325, 385, 450, 515);
     $cols = array(4, 94, 180, 300, 380, 450, 490);

	// $headers in doctext.inc
	$aligns = array('left',	'left',	'left',	'left', 'left', 'left', 'right', 'right','right');

	$params = array('comments' => $comments);

	$cur = get_company_Pref('curr_default');

	if ($email == 0)
	{
		if ($print_as_quote == 0)
		$rep = new FrontReport(_("SALES ORDER"), "SalesOrderBulk", user_pagesize());			
		else
			$rep = new FrontReport(_("QUOTE"), "QuoteBulk", user_pagesize());
		$rep->SetHeaderType('Header5');
		$rep->currency = $cur;
		$rep->Font();
		$rep->Info($params, $cols, null, $aligns);
	}

	for ($i = $from; $i <= $to; $i++)
	{
	
		$myrow = get_nota_pesan_header($i, ST_SALESORDER);//get_sales_order_item_details
		//$myrow4 = get_uang_muka($i);
		//$result_header = get_sales_order_header($i, ST_SALESORDER);		
		//$result = get_sales_order_details($i, ST_SALESORDER);
		$baccount = get_default_bank_account($myrow['curr_code']);		
		//$params['bankaccount'] = $baccount['id'];
		$branch = get_branch($myrow["branch_code"]);
		if ($email == 1)
		{
			$rep = new FrontReport("", "", user_pagesize());
			$rep->SetHeaderType('Header5');
			$rep->currency = $cur;
			$rep->Font();
			if ($print_as_quote == 1)
			{
				$rep->title = _('QUOTE');
				$rep->filename = "Quote" . $i . ".pdf";
			}
			else
			{
				$rep->title = _("SALES ORDER");
				$rep->filename = "SalesOrder" . $i . ".pdf";
			}
			$rep->Info($params, $cols, null, $aligns);
		}
		else
		$rep->title = ($print_as_quote==1 ? _("QUOTE") : _("SALES ORDER"));
		$contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no']);
		$rep->SetCommonData($myrow, $branch, $myrow, $baccount, ST_SALESORDERDETIL, $contacts);
		$rep->NewPage();
		
		

		
		$SubTotal = 0;
		$items = $prices = array();
		$x=0;
		
			$myrow3 = get_sales_order_item_details($myrow["id_np"]);
			//$x++;
		//	$Net = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
			//user_price_dec());
			//$prices[] = $Net;
			//$items[] = $myrow2['stk_code'];
			//$SubTotal += $Net;
			//$DisplayPrice = number_format2($myrow2["unit_price"],$dec);
			//$DisplayQty = number_format2($myrow2["quantity"],get_qty_dec($myrow2['stk_code']));
			//$DisplayNet = number_format2($Net,$dec);
			$size= ($myrow3['size_ss']+$myrow3['size_s']+$myrow3['size_m']+$myrow3['size_l']+
            $myrow3['size_ml']+$myrow3['size_xxl']+$myrow3['size_xxxl']+$myrow3['size_3l']+
            $myrow3['size_other']);
            
            $size_ss=$myrow3['size_ss'];
			$size_s=$myrow3['size_s'];
			$size_m=$myrow3['size_m'];
			$size_l=$myrow3['size_l'];
			$size_ml=$myrow3['size_ml'];
			$size_xxl=$myrow3['size_xxl'];
            $size_xxxl=$myrow3['size_xxxl'];
            $size_3l=$myrow3['size_3l'];
            $size_other=$myrow3['size_other'];
			
			$tot_ss=round2($myrow3['price_ss'] * $myrow3['size_ss']) ;
			$tot_s=round2($myrow3['price_s'] * $myrow3['size_s']) ;
			$tot_m=round2($myrow3['price_m'] * $myrow3['size_m']) ;
			$tot_l=round2($myrow3['price_l'] * $myrow3['size_l']) ;
			$tot_ml=round2($myrow3['price_ml'] * $myrow3['size_ml']) ;
			$tot_xxl=round2($myrow3['price_xxl'] * $myrow3['size_xxl']) ;
            $tot_xxxl=round2($myrow3['price_xxxl'] * $myrow3['size_xxxl']) ;
            $tot_3l=round2($myrow3['price_3l'] * $myrow3['size_3l']) ;
            $tot_other=round2($myrow3['price_other'] * $myrow3['size_other']) ;
			
			//$tot =round2($size * $myrow3["unit_price"]);
			
			//$total=number_format2($tot,$dec);
			
			
			$total_ss=number_format2($tot_ss,$dec);
			$total_s=number_format2($tot_s,$dec);
			$total_m=number_format2($tot_m,$dec);
			$total_l=number_format2($tot_l,$dec);
			$total_ml=number_format2($tot_ml,$dec);
			$total_xxl=number_format2($tot_xxl,$dec);
            $total_xxxl=number_format2($tot_xxxl,$dec);
            $total_3l=number_format2($tot_3l,$dec);
            $total_other=number_format2($tot_other,$dec);
			
			$SubTotal = $tot_ss+$tot_s+$tot_m+$tot_l+$tot_ml+$tot_xxl+$tot_xxxl
            +$tot_3l+$tot_other;
			
			//if ($myrow2["discount_percent"]==0)
//				$DisplayDiscount ="";
//			else
//			$DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
            
			//$rep->TextCol(0, 1,	$x, -2);  
			//$rep->TextCol(1, 2,	$myrow2['ref'], -2);
			
			//$oldrow = $rep->row;			
			//$rep->TextColLines(2, 3, $myrow2['description'], -2);
			//$newrow = $rep->row;
			//$rep->row = $oldrow;
			
			
		    $oldrow = $rep->row;			
			$rep->TextColLines(0, 1,	$myrow3['memo'], -2);
			$newrow = $rep->row;
			$rep->row = $oldrow;
			
			$oldrow = $rep->row;
			$rep->TextColLines(1, 2,	$myrow3['jns_brg'], -2);
			$newrow = $rep->row;
			$rep->row = $oldrow;
			
			$oldrow = $rep->row;			
			$rep->TextColLines(2, 3, $myrow3['color']."\n".$myrow3['kombinasi']."\n".$myrow3['sablon']."\n".$myrow3['model_jahit'], -2);
			$newrow = $rep->row;
			$rep->row = $oldrow;
			
			
			$oldrow = $rep->row;			
			$rep->TextColLines(3, 4, "SS"."\n"."S"."\n"."M"."\n"."L".
            "\n"."XL"."\n"."XXL"."\n"."XXXL"."\n"."3L"."\n"."Lainnya", -2);
			$newrow = $rep->row;
			$rep->row = $oldrow;
			
			$oldrow = $rep->row;			
			$rep->TextColLines(4, 5, $myrow3['size_ss']."\n".$myrow3['size_s'].
            "\n".$myrow3['size_m']."\n".$myrow3['size_l'].
            "\n".$myrow3['size_ml']."\n".$myrow3['size_xxl'].
            "\n".$myrow3['size_xxxl']."\n".$myrow3['size_3l'].
            "\n".$myrow3['size_other'], -2);
			$newrow = $rep->row;
			$rep->row = $oldrow;
			
			//$rep->TextCol(5, 6,	$total, -2);
			$oldrow = $rep->row;
			$rep->TextColLines(5, 6, number_format2($myrow3['price_ss'])."\n".
            number_format2($myrow3['price_s'])."\n".number_format2($myrow3['price_m']).
            "\n".number_format2($myrow3['price_l'])."\n".number_format2($myrow3['price_ml']).
            "\n".number_format2($myrow3['price_xxl'])."\n".number_format2($myrow3['price_xxxl']).
            "\n".number_format2($myrow3['price_3l'])."\n".number_format2($myrow3['price_other']), -2);
			$newrow = $rep->row;
			$rep->row = $oldrow;
			
			$oldrow = $rep->row;
			$rep->TextColLines(6, 7, $total_ss."\n".$total_s."\n".$total_m."\n".$total_l.
            "\n".$total_ml."\n".$total_xxl."\n".$total_xxxl."\n".$total_3l."\n".$total_other, -2);
			$newrow = $rep->row;
			$rep->row = $oldrow;
			
			//$rep->TextCol(6, 7,	$myrow3['size_s'], -2);
			//$rep->TextCol(7, 8,	$myrow3['size_m'], -2);
			//$rep->TextCol(8, 9,	$myrow3['size_l'], -2);
			//$rep->TextCol(9, 10,	$myrow3['size_ml'], -2);
			//$rep->TextCol(10, 11,	$myrow3['size_xxl'], -2);
			//$rep->TextCol(11, 12,	$subtotal, -2);
			//$rep->TextCol(3, 4,	$DisplayQty, -2);
			//$rep->TextCol(4, 5,	$myrow2['units'], -2);
		//	$rep->TextCol(5, 6,	$DisplayPrice, -2);
		//	$rep->TextCol(6, 7,	$DisplayDiscount, -2);
			//$rep->TextCol(7, 8,	$DisplayNet, -2);
			$rep->row = $newrow;
			//$rep->NewLine(1);
			if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
				$rep->NewPage();
				
		if ($myrow['comments'] != "")
		{
			$rep->NewLine();		
			$rep->TextColLines(1, 5, $myrow['comments'], -2);
		}
		$DisplaySubTot = number_format2($SubTotal,$dec);
		$DisplayFreight = number_format2($myrow["freight_cost"],$dec);

		$rep->row = $rep->bottomMargin + (18 * $rep->lineHeight);
		$doctype = ST_SALESORDER;
       // $um=$myrow4["pay1"];
		//$uangmuka=number_format2($um,$dec);
		
		$rep->TextCol(4, 8, _("Total Nilai Pesanan"), -2);
		$rep->TextCol(8, 9,	$DisplaySubTot, -2);
		$rep->NewLine();
		//$rep->TextCol(4, 8, _("Uang Muka"), -2);
		//$rep->TextCol(8, 9,	$uangmuka, -2);
		$rep->NewLine();

		$DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
//		if ($myrow['tax_included'] == 0) {
//			$rep->TextCol(3, 6, _("TOTAL ORDER EX VAT"), - 2);
//			$rep->TextCol(6, 7,	$DisplayTotal, -2);
//			$rep->NewLine();
//		}

		//$tax_items = get_tax_for_items($items, $prices, $myrow["freight_cost"],
//		$myrow['tax_group_id'], $myrow['tax_included'],  null);
//		$first = true;
//		foreach($tax_items as $tax_item)
//		{
//			if ($tax_item['Value'] == 0)
//				continue;
//			$DisplayTax = number_format2($tax_item['Value'], $dec);
//
//			$tax_type_name = $tax_item['tax_type_name'];
//
//			if ($myrow['tax_included'])
//			{
//				if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
//				{
//					if ($first)
//					{
//						$rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
//						$rep->TextCol(6, 7,	number_format2($sign*$tax_item['net_amount'], $dec), -2);
//						$rep->NewLine();
//					}
//					$rep->TextCol(3, 6, $tax_type_name, -2);
//					$rep->TextCol(6, 7,	$DisplayTax, -2);
//					$first = false;
//				}
//				else
//					$rep->TextCol(3, 7, _("Included") . " " . $tax_type_name . " " . _("Amount"). ": " . $DisplayTax, -2);
//			}
//			else
//			{
//				$SubTotal += $tax_item['Value'];
//				$rep->TextCol(3, 6, $tax_type_name, -2);
//				$rep->TextCol(6, 7,	$DisplayTax, -2);
//			}
//			$rep->NewLine();
//		}

		$rep->NewLine();

		//$DisplayTotal = number_format2($SubTotal-$um, $dec);
		$rep->Font('bold');
		//$rep->TextCol(3, 6, _("TOTAL ORDER VAT INCL."), - 2);
		
		//$rep->TextCol(4, 8, _("Sisa Tagihan"), - 2);
		//$rep->TextCol(8, 9,	$DisplayTotal, -2);
		$words = price_in_words($myrow["freight_cost"] + $SubTotal, ST_SALESORDER);
		if ($words != "")
		{
			$rep->NewLine(1);
			$rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
		}	
		$rep->Font();
		if ($email == 1)
		{
			$rep->End($email);
		}
		
	}
	if ($email == 0)
		$rep->End();
		
}

?>